package develop.iforce.jtest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver {
    public C2DMReceiver() {
        super("iforce.develop@gmail.com");
    }

    public void onRegistered(Context context, String registrationId) {
        Log.w("registration id:", registrationId);
        //sendMessage("id:" + registrationId);
    }

    @Override
    public void onUnregistered(Context context) {
        sendMessage("C2DM Unregistered");
    }

    @Override
    public void onError(Context context, String errorId) {
        sendMessage("err:" + errorId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {

    	Bundle extras = intent.getExtras();
    	receiveType = (String) extras.get("type");

    	Log.w("ww",receiveType);

    	if(receiveType.equals("message")){

    		receiveMessage = (String) extras.get("message");

    	}else if(receiveType.equals("earthquake")){

    		receiveLevel = (String) extras.get("level");
    		receiveSec = (String) extras.get("sec");
    		receiveTsunami = (String) extras.get("tsunami");

    		Log.w("ww","wwwwwwwwwwwwwwwwww");
    		Log.w("ww",receiveLevel);
    		Log.w("ww",receiveSec);
    		Log.w("ww",receiveTsunami);

    	}
    	sendMessage(receiveType);

    }

    public String receiveType;
    public String receiveMessage;
    public String receiveLevel;
    public String receiveSec;
    public String receiveTsunami;

    private void sendMessage(String str) {
        Message mes = Message.obtain(JschaActivity.mH);
        Bundle data = mes.getData();
        data.putBoolean("receivedMessageFlag", true);
        data.putString("receivedTypeString", receiveType);
        data.putString("receivedMessageString", receiveMessage);
        data.putString("receivedLevelString", receiveLevel);
        data.putString("receivedSecString", receiveSec);
        data.putString("receivedTsunamiString", receiveTsunami);

        JschaActivity.mH.sendMessage(mes);
        mes = null;
    }
 }